package org.jeecg.modules.flower.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.ChineseCharacterUtil;
import org.jeecg.modules.chvegetable.entity.FlowerVarietyZZ;
import org.jeecg.modules.chvegetable.service.IFlowerVarietyZZService;
import org.jeecg.modules.flower.entity.FlowerVariety;
import org.jeecg.modules.flower.service.IFlowerVarietyService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.flower.util.CommonUtil;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 蔬菜品种表
 * @Author: jeecg-boot
 * @Date:   2022-06-09
 * @Version: V1.0
 */
@Api(tags="蔬菜品种表")
@RestController
@RequestMapping("/flower/flowerVariety")
@Slf4j
public class FlowerVarietyController extends JeecgController<FlowerVariety, IFlowerVarietyService> {
	@Autowired
	private IFlowerVarietyService flowerVarietyService;
	@Autowired
	private IFlowerVarietyZZService flowerVarietyZZService;

	/**
	 * 分页列表查询
	 *
	 * @param flowerVariety
	 * @param
	 * @param req
	 * @return
	 */
	@AutoLog(value = "蔬菜品种表-不分页查询")
	@ApiOperation(value="蔬菜品种表-不分页查询", notes="蔬菜品种表-不分页查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(FlowerVariety flowerVariety,
								   HttpServletRequest req) {
//		QueryWrapper<FlowerVariety> queryWrapper = QueryGenerator.initQueryWrapper(flowerVariety, req.getParameterMap());
		LambdaQueryWrapper<FlowerVariety> queryWrapper =new LambdaQueryWrapper<>();
		if(StringUtils.isNotBlank(flowerVariety.getZzid())){
			queryWrapper.eq(FlowerVariety::getZzid,flowerVariety.getZzid());
		}
		if(StringUtils.isNotBlank(flowerVariety.getName())){
			queryWrapper.like(FlowerVariety::getName,flowerVariety.getName());
		}
		if(StringUtils.isNotBlank(flowerVariety.getCode())){
			queryWrapper.like(FlowerVariety::getCode ,flowerVariety.getCode());
		}
		if(StringUtils.isNotBlank(flowerVariety.getFlowerPoleLength())){
			queryWrapper.like(FlowerVariety::getFlowerPoleLength ,flowerVariety.getFlowerPoleLength());
		}
		if(StringUtils.isNotBlank(flowerVariety.getPackingSpecifications())){
			queryWrapper.like(FlowerVariety::getPackingSpecifications ,flowerVariety.getPackingSpecifications());
		}
		if(flowerVariety.getState() != null){
			queryWrapper.eq(FlowerVariety::getState ,flowerVariety.getState());
		}
		queryWrapper.orderByAsc(FlowerVariety::getCode);
		queryWrapper.orderByAsc(FlowerVariety::getName);
		List<FlowerVariety> list = flowerVarietyService.list(queryWrapper);
		return Result.ok(list);
	}

	@AutoLog(value = "蔬菜品种表-分页列表查询")
	@ApiOperation(value="蔬菜品种表-分页列表查询", notes="蔬菜品种表-分页列表查询")
	@GetMapping(value = "/list2")
	public Result<?> queryPageList2(FlowerVariety flowerVariety,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
//		QueryWrapper<FlowerVariety> queryWrapper = QueryGenerator.initQueryWrapper(flowerVariety, req.getParameterMap());
		Page<FlowerVariety> page = new Page<FlowerVariety>(pageNo, pageSize);
		LambdaQueryWrapper<FlowerVariety> queryWrapper =new LambdaQueryWrapper<>();
		if(StringUtils.isNotBlank(flowerVariety.getZzid())){
			queryWrapper.eq(FlowerVariety::getZzid,flowerVariety.getZzid());
		}
		if(StringUtils.isNotBlank(flowerVariety.getName())){
			queryWrapper.like(FlowerVariety::getName,flowerVariety.getName());
		}
		if(StringUtils.isNotBlank(flowerVariety.getCode())){
			queryWrapper.like(FlowerVariety::getCode ,flowerVariety.getCode());
		}
		if(StringUtils.isNotBlank(flowerVariety.getFlowerPoleLength())){
			queryWrapper.like(FlowerVariety::getFlowerPoleLength ,flowerVariety.getFlowerPoleLength());
		}
		if(StringUtils.isNotBlank(flowerVariety.getPackingSpecifications())){
			queryWrapper.like(FlowerVariety::getPackingSpecifications ,flowerVariety.getPackingSpecifications());
		}
		if(flowerVariety.getState() != null){
			queryWrapper.eq(FlowerVariety::getState ,flowerVariety.getState());
		}
		queryWrapper.orderByAsc(FlowerVariety::getCode);
		queryWrapper.orderByAsc(FlowerVariety::getName);
		IPage<FlowerVariety> pageList = flowerVarietyService.page(page, queryWrapper);
		return Result.ok(pageList);
	}


	 @AutoLog(value = "蔬菜品种名称--列表查询")
	 @ApiOperation(value="蔬菜品种名称--列表查询", notes="蔬菜品种名称--列表查询")
	 @GetMapping(value = "/queryNameLists")
	 public Result<?> queryNameLists(FlowerVariety flowerVariety ) {

		 LambdaQueryWrapper<FlowerVariety> queryWrapper =new LambdaQueryWrapper<>();
		 if(StringUtils.isNotBlank(flowerVariety.getZzid())){
			 queryWrapper.eq(FlowerVariety::getZzid,flowerVariety.getZzid());
		 }
		 if(StringUtils.isNotBlank(flowerVariety.getName())){
			 queryWrapper.like(FlowerVariety::getName,flowerVariety.getName());
		 }
		 if(StringUtils.isNotBlank(flowerVariety.getCode())){
			 queryWrapper.like(FlowerVariety::getCode ,flowerVariety.getCode());
		 }
		 if(StringUtils.isNotBlank(flowerVariety.getFlowerPoleLength())){
			 queryWrapper.like(FlowerVariety::getFlowerPoleLength ,flowerVariety.getFlowerPoleLength());
		 }
		 if(StringUtils.isNotBlank(flowerVariety.getPackingSpecifications())){
			 queryWrapper.like(FlowerVariety::getPackingSpecifications ,flowerVariety.getPackingSpecifications());
		 }
		 if(flowerVariety.getState() != null){
			 queryWrapper.eq(FlowerVariety::getState ,flowerVariety.getState());
		 }
		 queryWrapper.select(FlowerVariety::getName);
		 queryWrapper.groupBy(FlowerVariety::getName);
		 queryWrapper.orderByAsc(FlowerVariety::getName);
		 List<FlowerVariety> pageList = flowerVarietyService.list(queryWrapper);
//		 List<String> list =new ArrayList<>();
//		 for(FlowerVariety variety: pageList){
//			 list.add(variety.getName());
//		 }
		 return Result.ok(pageList);
	 }

	/**
	 *   添加
	 *
	 * @param flowerVariety
	 * @return
	 */
	@AutoLog(value = "蔬菜品种表-添加")
	@ApiOperation(value="蔬菜品种表-添加", notes="蔬菜品种表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody FlowerVariety flowerVariety) {
		String code = flowerVariety.getCode();
		//获取名字
		String zzid = flowerVariety.getZzid();
		if(StringUtils.isBlank(zzid)) {
			throw new RuntimeException("种植id为空，请重试！");
		}
		FlowerVarietyZZ flowervarzz = flowerVarietyZZService.getById(zzid);
		if(flowervarzz == null || StringUtils.isBlank(flowervarzz.getName())) {
			throw new RuntimeException("未找到数据！flowerVarietyZZS："+ zzid);
		}
		flowerVariety.setName(flowervarzz.getName());
		//获取名字--end

		//自动生成编码
		if(StringUtils.isBlank( code )){
			flowerVariety.setCode( createCode(flowerVariety.getName()) );
		}
		QueryWrapper<FlowerVariety> queryWrapper =new QueryWrapper<>();
		queryWrapper.eq("code",flowerVariety.getCode());
		List<FlowerVariety> list = flowerVarietyService.list(queryWrapper);
		if(list.size()>0){
			throw  new RuntimeException(code+"编码已经被占用，请修改编码");
		}
		flowerVarietyService.save(flowerVariety);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param flowerVariety
	 * @return
	 */
	@AutoLog(value = "蔬菜品种表-编辑")
	@ApiOperation(value="蔬菜品种表-编辑", notes="蔬菜品种表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody FlowerVariety flowerVariety) {
		String code = flowerVariety.getCode();
		QueryWrapper<FlowerVariety> queryWrapper =new QueryWrapper<>();
		queryWrapper.eq("code",code);
		queryWrapper.ne("id",flowerVariety.getId());
		List<FlowerVariety> list = flowerVarietyService.list(queryWrapper);
		if(list.size()>0){
			throw  new RuntimeException(code+"编码已经被占用，请修改编码");
		}
		//获取名字
		String zzid = flowerVariety.getZzid();
		if(StringUtils.isBlank(zzid)) {
			throw new RuntimeException("种植id为空，请重试！");
		}
		FlowerVarietyZZ flowervarzz = flowerVarietyZZService.getById(zzid);
		if(flowervarzz == null || StringUtils.isBlank(flowervarzz.getName())) {
			throw new RuntimeException("未找到数据！flowerVarietyZZS："+ zzid);
		}
		flowerVariety.setName(flowervarzz.getName());
		//获取名字--end

		flowerVarietyService.updateById(flowerVariety);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "蔬菜品种表-通过id删除")
	@ApiOperation(value="蔬菜品种表-通过id删除", notes="蔬菜品种表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		flowerVarietyService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "蔬菜品种表-批量删除")
	@ApiOperation(value="蔬菜品种表-批量删除", notes="蔬菜品种表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.flowerVarietyService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "蔬菜品种表-通过id查询")
	@ApiOperation(value="蔬菜品种表-通过id查询", notes="蔬菜品种表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		FlowerVariety flowerVariety = flowerVarietyService.getById(id);
		if(flowerVariety==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(flowerVariety);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param flowerVariety
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FlowerVariety flowerVariety) {
        return super.exportXls(request, flowerVariety, FlowerVariety.class, "蔬菜品种表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, FlowerVariety.class);
    }



	 @GetMapping(value = "/getFlowerType")
	 public Result<?> getFlowerType(){
		 QueryWrapper<FlowerVariety> queryWrapper = new QueryWrapper<>();
		 queryWrapper.select("name value","name text","name title");
		 queryWrapper.groupBy("name");
		 List<Map<String, Object>> maps = this.flowerVarietyService.listMaps(queryWrapper);
		 return Result.ok(maps);
	 }
	 @GetMapping(value = "/getPackingSpecifications")
	 public Result<?> getPackingSpecifications(){
		 QueryWrapper<FlowerVariety> queryWrapper = new QueryWrapper<>();
		 queryWrapper.select("packing_specifications value","packing_specifications text","packing_specifications title");
		 queryWrapper.groupBy("packing_specifications");
		 List<Map<String, Object>> maps = this.flowerVarietyService.listMaps(queryWrapper);
		 return Result.ok(maps);
	 }


	 /**
	  * -分组查询名称，用于下拉
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "蔬菜品种表-分组查询名称，用于下拉")
	 @ApiOperation(value="蔬菜品种表--分组查询名称，用于下拉", notes="蔬菜品种表--分组查询名称，用于下拉")
	 @GetMapping(value = "/queryNames")
	 public Result<?> queryNames( ) {
		 QueryWrapper<FlowerVariety> queryWrapper = new QueryWrapper<>();
		 queryWrapper.groupBy("name");
		 queryWrapper.orderByAsc("name");
		 List<FlowerVariety> flowerVarietyList = flowerVarietyService.list(queryWrapper);
		 return Result.ok(flowerVarietyList);
	 }
	 /**
	  * -根据名称查询规格，用于下拉
	  *
	  * @param name
	  * @return
	  */
	 @AutoLog(value = "蔬菜品种表-根据名称查询规格")
	 @ApiOperation(value="蔬菜品种表--根据名称查询规格，用于下拉", notes="蔬菜品种表--根据名称查询规格，用于下拉")
	 @GetMapping(value = "/queryPacksByName")
	 public Result<?> queryPacksByName(@RequestParam("name") String name ) {
		 QueryWrapper<FlowerVariety> queryWrapper = new QueryWrapper<>();
		 queryWrapper.select("packing_specifications");
		 queryWrapper.eq("name",name);
		 queryWrapper.groupBy("packing_specifications");
		 queryWrapper.orderByAsc("packing_specifications");
		 List<FlowerVariety> flowerVarietyList = flowerVarietyService.list(queryWrapper);
		 return Result.ok(flowerVarietyList);
	 }

	 @AutoLog(value = "蔬菜品种表-分组查询等级-根据名称、规格")
	 @ApiOperation(value="蔬菜品种表--分组查询等级-根据名称、规格，用于下拉", notes="蔬菜品种表--分组查询等级-根据名称、规格，用于下拉")
	 @GetMapping(value = "/queryClassesByName")
	 public Result<?> queryClassesByName(@RequestParam("name") String name , @RequestParam("packing") String packing) {
		 QueryWrapper<FlowerVariety> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("name",name);
		 queryWrapper.eq("packing_specifications",packing);
		 queryWrapper.select("classes");
		 queryWrapper.groupBy("classes");
		 queryWrapper.orderByAsc("classes");
		 List<FlowerVariety> flowerVarietyList = flowerVarietyService.list(queryWrapper);
		 return Result.ok(flowerVarietyList);
	 }

	 /**
	  * 分组查询长度，用于下拉
	  *
	  * @param name
	  * @return
	  */
	 @AutoLog(value = "蔬菜品种表-分组查询单位")
	 @ApiOperation(value="蔬菜品种表--分组查询单位，用于下拉", notes="蔬菜品种表--分组查询单位，用于下拉")
	 @GetMapping(value = "/queryLengthByName")
	 public Result<?> queryLengthByName(@RequestParam("name") String name , @RequestParam("packing") String packing, @RequestParam("classes") String classes) {
		 QueryWrapper<FlowerVariety> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("name",name);
		 queryWrapper.eq("packing_specifications",packing);
		 queryWrapper.eq("classes",classes);
		 queryWrapper.select("flower_pole_length");
		 queryWrapper.groupBy("flower_pole_length");
		 queryWrapper.orderByAsc("flower_pole_length");
		 List<FlowerVariety> flowerVarietyList = flowerVarietyService.list(queryWrapper);
		 return Result.ok(flowerVarietyList);
	 }


	 private String createCode (String name) {
		 //自动生成出库单号
		 Integer leng = 3;
		 String refex = "YTH-" + ChineseCharacterUtil.getUpperCase(name, false) + "-";
		 LambdaQueryWrapper<FlowerVariety> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		 lambdaQueryWrapper.likeRight(FlowerVariety::getCode, refex);
		 lambdaQueryWrapper.orderByDesc(FlowerVariety::getCode);
		 List<FlowerVariety> list = flowerVarietyService.list(lambdaQueryWrapper);
		 String oldNo = "";
		 if (list.size() > 0) {
			 oldNo = list.get(0).getCode();
		 }
		 Integer n = 1;
		 if (StringUtils.isNotBlank(oldNo) && oldNo.length() > leng) {
			 String oldNoNum = oldNo.substring(oldNo.length() - leng, oldNo.length());
			 n = Integer.parseInt(oldNoNum) + 1;
		 }
		 String newFlowNum = CommonUtil.subLength(n, leng);
		 return refex + newFlowNum;
	 }

}
